﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
using System.Xml;

namespace GiaoVien_QuanLy_DeTai.NewFolder1
{
    public class CreatConnection
    {
        private static SqlConnection sqlConnection;
        private static string stringConnection;
        private SqlCommand sqlCommand;
       // private SqlDataAdapter sqlDataAdapter;

        public CreatConnection()
        {
          
        }


        public void openConnecttion()
        {
            if (sqlConnection.State == ConnectionState.Open)
            {
                try
                {
                    stringConnection = "";
                    sqlConnection = new SqlConnection(stringConnection);
                    sqlConnection.Open();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            else
                return;
        }


        public void closeConnection()
        {
            if (sqlConnection.State == ConnectionState.Open)
            {
                sqlConnection.Close();
                sqlConnection.Dispose();
            }
            else
                return;
        }

        public void executeNonQuery(string executeNonQueryString)
        {
            sqlCommand = new SqlCommand();
            try
            {
                openConnecttion();
                sqlCommand.CommandType = CommandType.Text;
                sqlCommand.CommandText = executeNonQueryString;
                sqlCommand.Connection = sqlConnection;

                int record = 0;
                record = sqlCommand.ExecuteNonQuery();

                if (record >= 1)
                {
                    MessageBox.Show("Thuc Hien Thanh Cong");
                }

                else
                    MessageBox.Show("Thuc Hien That Bai");
                closeConnection();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }          
        }

        public object executeScalar(string executeScalarString)
        {
            object record = 0 ;
            sqlCommand = new SqlCommand();
            try
            {
                openConnecttion();
                sqlCommand.CommandType = CommandType.Text;
                sqlCommand.CommandText = executeScalarString;
                sqlCommand.Connection = sqlConnection;

                
                record = sqlCommand.ExecuteScalar();
                closeConnection();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            return record;
        }

        public SqlDataReader sqlDataReader(string sqlDataReaderString)
        {
            SqlDataReader reader = null;
            sqlCommand = new SqlCommand();
            try
            {
                openConnecttion();
                sqlCommand.CommandType = CommandType.Text;
                sqlCommand.CommandText = sqlDataReaderString;
                sqlCommand.Connection = sqlConnection;


                reader = sqlCommand.ExecuteReader();
                closeConnection();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            return reader;
        }
        

    }
}
